﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Linq;
using System.Data;

namespace JM.JCY.SharePointComponent.LawArchives.PeopleSuperviseDetails
{
    public partial class PeopleSuperviseDetailsUserControl : UserControl
    {
        T_AJBSGXDataSet.T_AJBSGXDataTable AJBSGXData;
        T_RMJDY_JDAJXXDataSet.T_RMJDY_ALLAYInfoDataTable AllAYData;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string N_BH = Request.QueryString["N_BH"].ToString();
                this.lbBH.Text = N_BH;
                AllAYData = GetRMJDY_XYRAYData(Convert.ToDecimal(N_BH));
                var RMJDYDT = GetRMJDYDataBH(Convert.ToDecimal(N_BH));
                BindData(RMJDYDT);
                AJBSGXData = GetAJBSGXDataByBH(Convert.ToDecimal(N_BH));
                lbBATS.Text = AJBSGXData.Count > 0 ? AJBSGXData[0].N_BATS.ToString() : "0";

              
                DataRow[] xYRRows = GetRMJDY_XYRDataBH(Convert.ToDecimal(N_BH)).Select();
                dlXYR.DataSource = xYRRows;
                dlXYR.DataBind();
                if (xYRRows.Count() > 0)
                {

                    XYRNoRecord.Visible = false;
                }

                else
                    XYRNoRecord.Visible = true;
            }
        }
        public T_AJBSGXDataSet.T_AJBSGXDataTable GetAJBSGXDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJBSGXDataByBH(BH);
            }
        }
        public T_RMJDY_JDAJXXDataSet.T_RMJDY_JDAJXXDataTable GetRMJDYDataBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetRMJDYDataBH(BH);
            }
        }

        public T_RMJDY_JDAJXXDataSet.T_RYXX20100322DataTable GetRMJDYData(decimal RYBH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetRYXXByRYBH(RYBH);
            }
        }

        public T_RMJDY_JDAJXXDataSet.T_RMJDY_XYRXXDataTable GetRMJDY_XYRDataBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetRMJDY_XYRDataBH(BH);
            }
        }
        public void BindData( T_RMJDY_JDAJXXDataSet.T_RMJDY_JDAJXXDataTable DT)
        {
            var row = DT[0];
            this.lbID.Text = string.IsNullOrEmpty(row.C_AH) ? string.Empty : row.C_AH.ToString();
            var DLR = string.Empty;
            if (!row.IsN_ZSARNull())
            {
                var dt = GetRMJDYData(row.N_ZSAR);
                if (dt.Count > 0)
                {
                    DLR = dt[0].C_XM;
                }
            }
            this.lbPerson.Text = DLR;
            lbGetDate.Text = row.IsD_SARQNull() ? string.Empty : row.D_SARQ.ToString("yyyy年MM月dd日");

            var JDLXDM = GetDMByBXH(2201);
            var JDLX = from dm in JDLXDM
                       where !row.IsN_JDLXNull() && dm.N_DM == row.N_JDLX
                       select dm.C_MC;
            lbType.Text = JDLX.Count() > 0 ? string.Empty : JDLX.First();
            //TODO处理时间
            //lbHandleDate.Text=
        }

        //Method to get the data in table T_DM
        public T_DMDataSet.T_DMDataTable GetDMByBXH(decimal BXH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDMByBXH(BXH);
            }
        }
        public T_RMJDY_JDAJXXDataSet.T_RMJDY_ALLAYInfoDataTable GetRMJDY_XYRAYData(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetRMJDY_XYRAYData(BH);
            }
        }
        protected void dlXYR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_RMJDY_JDAJXXDataSet.T_RMJDY_XYRXXRow row = (T_RMJDY_JDAJXXDataSet.T_RMJDY_XYRXXRow)e.Item.DataItem;
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbGender = (Label)e.Item.FindControl("lbGender");
            Label lbReason = (Label)e.Item.FindControl("lbReason");
            lbName.Text = string.IsNullOrEmpty(row.C_XM) ? string.Empty : row.C_XM;
            lbGender.Text = row.IsN_XBNull() ? string.Empty : row.N_XB == 1 ? "男" : "女";
            var LAAY = from all in AllAYData
                       where all.N_LX == 1 && !string.IsNullOrEmpty(row.C_XM) && all.C_XM == row.C_XM
                       select all.C_MC;
            string reason = string.Empty;
            int i = 0;
            foreach (var item in LAAY)
            {
                if (i < LAAY.Count() - 1)
                {
                    reason += item + "、";
                }
                else
                {
                    reason += item;
                }
                i++;
            }
            lbReason.Text = reason;
        }
    }
}
